﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;


public partial class webprint_print01 : System.Web.UI.Page
{
    PrintDocument pDoc = new PrintDocument();
    bool isPerPrint = false;
    string filename = "";
    
    DBO.delivery_order_outside outside_order = new DBO.delivery_order_outside();
    BLL.delivery_order_outside_BLL outside_bll = new BLL.delivery_order_outside_BLL();
    DBO.delivery_order_local local_order = new DBO.delivery_order_local();
    BLL.delivery_order_local_BLL local_bll = new BLL.delivery_order_local_BLL();
    
    protected void Page_Load(object sender, EventArgs e)
    {

        pDoc.PrintPage += new PrintPageEventHandler(pDoc_PrintPage);
        pDoc.QueryPageSettings += new QueryPageSettingsEventHandler(pDoc_QueryPageSettings);
        pDoc.BeginPrint += new PrintEventHandler(pDoc_BeginPrint);

    }
    
    public string SaveInputs()
    {
        string ordertype = Request.Form["rdtype"].ToString();
        if (ordertype != null)
        {
            decimal decvalue;
            Int32 intvalue;
            if (ordertype == "0")
            {
                decimal.TryParse(txtCharges.Text, out decvalue);
                outside_order.charges = decvalue;
                outside_order.create_date = System.DateTime.Today;
                outside_order.from_add = txtFromAdd.Text;
                outside_order.from_company = txtFromCompany.Text;
                outside_order.from_linkman = txtFromLinkMan.Text;
                outside_order.from_tel = txtFromTel.Text;
                outside_order.good_name = txtGoodName.Text;
                decimal.TryParse(txtGoodPrice.Value, out decvalue);
                outside_order.good_price = decvalue;
                decimal.TryParse(txtInsuranceAmount.Text, out decvalue);
                outside_order.insurance_amount = decvalue;
                decimal.TryParse(txtInsuranceFee.Text, out decvalue);
                outside_order.insurance_fee = decvalue;
                outside_order.no_end = txtNoEnd.Text;
                outside_order.no_start = txtNoStart.Text;
                outside_order.order_id = Guid.NewGuid().ToString();
                if (Int32.TryParse(rblPayType.SelectedValue, out intvalue))
                    outside_order.pay_type = intvalue;
                else
                    outside_order.pieces = -1;
                Int32.TryParse(txtPieces.Text, out intvalue);
                outside_order.pieces = intvalue;
                outside_order.remark = txtRemark.Text;
                outside_order.return_no = txtReturnNo.Text;
                outside_order.to_add = txtToAdd.Text;
                outside_order.to_company = txtToCompany.Text;
                outside_order.to_linkman = txtToLinkMan.Text;
                outside_order.to_tel = txtToTel.Text;
                decimal.TryParse(txtVolume.Text, out decvalue);
                outside_order.volume = decvalue;
                decimal.TryParse(txtWeight.Text, out decvalue);
                outside_order.weight = decvalue;
                outside_bll.Add(outside_order);
            }
            else
            {
                local_order.contents = txt_contents.Text;
                local_order.create_date = System.DateTime.Today;
                local_order.from_add = txt_FromAdd.Text;
                local_order.from_company = txt_FromCompany.Text;
                local_order.from_tel = txt_FromTel.Text;
                decimal.TryParse(txt_Insurance.Text, out decvalue);
                local_order.insurance = decvalue;
                if (Int32.TryParse(rbl_return.SelectedValue, out intvalue))
                    local_order.is_return = intvalue;
                else
                    local_order.is_return = -1;
                local_order.order_id = Guid.NewGuid().ToString();
                if (Int32.TryParse(rbl_paytype.SelectedValue, out intvalue))
                    local_order.pay_type = intvalue;
                else
                    local_order.pay_type = -1;
                Int32.TryParse(txt_Pieces.Text, out intvalue);
                local_order.pieces = intvalue;
                local_order.receiver = txt_Receiver.Text;
                local_order.remark = txt_Remark.Text;
                local_order.sender = txt_Sender.Text;
                local_order.to_add = txt_ToAdd.Text;
                local_order.to_company = txt_ToCompany.Text;
                local_order.to_tel = txt_ToTel.Text;
                decimal.TryParse(txt_TotalPrice.Text, out decvalue);
                local_order.total_price = decvalue;
                decimal.TryParse(txt_Urgent.Text, out decvalue);
                local_order.urgent = decvalue;
                decimal.TryParse(txt_Weight.Text, out decvalue);
                local_order.weight = decvalue;
                local_bll.Add(local_order);

            }
        }
        return ordertype;
    }

    void pDoc_BeginPrint(object sender, PrintEventArgs e)
    {

        if (isPerPrint)
        {
            pDoc.PrinterSettings.PrintToFile = true;
            filename = Server.MapPath(".") + "\\" + Guid.NewGuid().ToString() + ".jpg";
            pDoc.PrinterSettings.PrintFileName = filename;
        }

    }


    void pDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
    {
        e.PageSettings.PaperSize = new PaperSize("外阜快递单", 941, 500);
        e.PageSettings.PrinterResolution.Kind = PrinterResolutionKind.Low;
        e.PageSettings.Landscape = true;
    }

    void pDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        string ordertype = SaveInputs();
        Font font = new Font("Arial", 10, System.Drawing.FontStyle.Regular);
        if (ordertype != null)
            if (ordertype == "0")
            {
                e.Graphics.DrawString(outside_order.from_company, font, Brushes.Black, (float)118, (float)130);
                outside_order.from_add = NewLine(outside_order.from_add, 8);
                e.Graphics.DrawString(outside_order.from_add, font, Brushes.Black, (float)118, (float)160);
                e.Graphics.DrawString(outside_order.from_linkman, font, Brushes.Black, (float)118, (float)236);
                e.Graphics.DrawString(outside_order.from_tel, font, Brushes.Black, (float)217, (float)236);
                e.Graphics.DrawString(outside_order.to_company, font, Brushes.Black, (float)382, (float)130);
                outside_order.to_add = NewLine(outside_order.to_add, 8);
                e.Graphics.DrawString(outside_order.to_add, font, Brushes.Black, (float)382, (float)160);
                e.Graphics.DrawString(outside_order.to_linkman, font, Brushes.Black, (float)382, (float)236);
                e.Graphics.DrawString(outside_order.to_tel, font, Brushes.Black, (float)484, (float)236);
                e.Graphics.DrawString(outside_order.insurance_amount.ToString(), font, Brushes.Black, (float)125, (float)270);
                e.Graphics.DrawString(outside_order.good_name, font, Brushes.Black, (float)125, (float)290);
                e.Graphics.DrawString(outside_order.pieces.ToString(), font, Brushes.Black, (float)125, (float)354);
                e.Graphics.DrawString(outside_order.insurance_fee.ToString(), font, Brushes.Black, (float)276, (float)270);
                e.Graphics.DrawString(outside_order.volume.ToString(), font, Brushes.Black, (float)276, (float)290);
                e.Graphics.DrawString(outside_order.weight.ToString(), font, Brushes.Black, (float)276, (float)354);

                e.Graphics.DrawString(outside_order.charges.ToString(), font, Brushes.Black, (float)354, (float)354);
                outside_order.remark = NewLine(outside_order.remark, 8);
                e.Graphics.DrawString(outside_order.remark, font, Brushes.Black, (float)630, (float)177);
                e.Graphics.DrawString(outside_order.good_price.ToString(), font, Brushes.Black, (float)708, (float)270);
                e.Graphics.DrawString(outside_order.return_no, font, Brushes.Black, (float)689, (float)290);
                e.Graphics.DrawString(outside_order.no_start, font, Brushes.Black, (float)689, (float)326);
                e.Graphics.DrawString(outside_order.no_end, font, Brushes.Black, (float)689, (float)354);
            }
            else
            {
                e.Graphics.DrawString(local_order.sender, font, Brushes.Black, (float)118, (float)123);
                e.Graphics.DrawString(local_order.from_company, font, Brushes.Black, (float)118, (float)179);
                e.Graphics.DrawString(local_order.from_add, font, Brushes.Black, (float)118, (float)266);
                e.Graphics.DrawString(local_order.from_tel, font, Brushes.Black, (float)118, (float)336);
                e.Graphics.DrawString(local_order.contents, font, Brushes.Black, (float)98, (float)423);
                e.Graphics.DrawString(local_order.receiver, font, Brushes.Black, (float)433, (float)123);
                e.Graphics.DrawString(local_order.to_company, font, Brushes.Black, (float)433, (float)179);
                e.Graphics.DrawString(local_order.to_add, font, Brushes.Black, (float)433, (float)266);
                e.Graphics.DrawString(local_order.to_tel, font, Brushes.Black, (float)433, (float)336);
                e.Graphics.DrawString(local_order.pieces.ToString(), font, Brushes.Black, (float)768, (float)123);
                e.Graphics.DrawString(local_order.weight.ToString(), font, Brushes.Black, (float)768, (float)179);
                e.Graphics.DrawString(local_order.urgent.ToString(), font, Brushes.Black, (float)768, (float)226);
                e.Graphics.DrawString(local_order.insurance.ToString(), font, Brushes.Black, (float)799, (float)285);
                e.Graphics.DrawString(local_order.total_price.ToString(), font, Brushes.Black, (float)768, (float)336);
                local_order.remark = NewLine(local_order.remark, 10);
                e.Graphics.DrawString(local_order.remark, font, Brushes.Black, (float)669, (float)413);
            }
    }
    protected void btPrePrint_Click(object sender, EventArgs e)
    {
        isPerPrint = true;
        pDoc.Print();
    }
    protected void btPrint_Click(object sender, EventArgs e)
    {

        pDoc.Print();
    }
    private string NewLine(string str, int size)
    {
        string tempStr = "";
        if (str.Length > 0 && str.Length >= size)
        {
            for (int i = 0; i < str.Length / size; i++)
            {
                tempStr += str.Substring(i * size, size) + "\n";
            }
        }
        return tempStr;

    }
}
